package fy.uap.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.json.JSONArray;
import org.json.JSONObject;

import fy.util.DBUtil;

public class FillSupplierInfoSelectDao {

	private String cvenbankSelect;// 银行

	private String couttypeSelect;// 物流方式

	private String cmoneytypeSelect;// 币种

	private String companyTypeSelect;// 公司类型

	private String aptitudeClassSelect;// 供应商资质类别

	public String getAptitudeClassSelect() {
		return aptitudeClassSelect;
	}

	public void querySelectOption(Connection conn) throws Exception {

		if (cvenbankSelect == null || couttypeSelect == null
				|| cmoneytypeSelect == null || companyTypeSelect == null) {
			Statement stat = null;
			try {
				stat = conn.createStatement();
				queryCvenbankSelectOption(stat);
				queryCouttypeSelectOption(stat);
				queryMoneyTypeSelectOption(stat);
				queryCompanySelectOption(stat);
//				try {
//					queryAptitudeClassSelectOption(stat);
//					// queryCompanyTypeSelectOption(stat);
//				} catch (Exception e) {
//					
//				}
			} finally {
				DBUtil.close(stat);
			}
		}
	}

	private void queryCvenbankSelectOption(Statement stat) throws Exception {
		// select b.banktypecode, b.banktypename from bd_banktype b;
		ResultSet rs = null;
		try {
			rs = stat
					.executeQuery("select b.pk_banktype, b.banktypename from bd_banktype b where (b.dr='0' or b.dr is null) ");
			StringBuilder sb = new StringBuilder();
			while (rs.next()) {
				sb.append("<option value='").append(rs.getString(1))
						.append("'>").append(rs.getString(2))
						.append("</option>");
			}
			cvenbankSelect = sb.toString();
		} finally {
			DBUtil.close(rs);
		}
	}

	private void queryCouttypeSelectOption(Statement stat) throws Exception {

		ResultSet rs = null;
		try {
			// select s.sendcode, s.sendname from bd_sendtype s
			rs = stat
					.executeQuery("select s.pk_sendtype, s.sendname from bd_sendtype s where (s.dr='0' or s.dr is null)");
			StringBuilder sb = new StringBuilder();
			while (rs.next()) {
				sb.append("<option value='").append(rs.getString(1))
						.append("'>").append(rs.getString(2))
						.append("</option>");
			}
			couttypeSelect = sb.toString();
		} finally {
			DBUtil.close(rs);
		}
	}

	private void queryMoneyTypeSelectOption(Statement stat) throws Exception {

		ResultSet rs = null;
		try {
			// select c.currtypecode, c.currtypename from bd_currtype c
			rs = stat
					.executeQuery("select c.pk_currtype, c.currtypename from bd_currtype c where (c.dr='0' or c.dr is null) ");
			StringBuilder sb = new StringBuilder();
			while (rs.next()) {
				sb.append("<option value='").append(rs.getString(1))
						.append("'>").append(rs.getString(2))
						.append("</option>");
			}
			cmoneytypeSelect = sb.toString();
		} finally {
			DBUtil.close(rs);
		}
	}

	private void queryCompanySelectOption(Statement stat) throws Exception {
		if (CompanyTypeDao.getInstance().getCompanyTypeList() != null) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0, size = CompanyTypeDao.getInstance()
					.getCompanyTypeList().size(); i < size; i++) {
				sb.append("<option value='")
						.append(CompanyTypeDao.getInstance()
								.getCompanyTypeList().get(i)[0])
						.append("'>")
						.append(CompanyTypeDao.getInstance()
								.getCompanyTypeList().get(i)[1])
						.append("</option>");
			}
			companyTypeSelect = sb.toString();
		}

	}

//	private void queryAptitudeClassSelectOption(Statement stat)
//			throws Exception {
//		ResultSet rs = null;
//		try {
//			// select c.currtypecode, c.currtypename from bd_currtype c
//			rs = stat
//					.executeQuery("select c.pk_aptclas, c.name,c.pk_parent from fy_supplier_aptitude_class c where nvl(c.dr,0) = 0 order by c.name asc");
//			JSONArray ja = new JSONArray();
//			while (rs.next()) {
//				JSONObject jo = new JSONObject();
//				jo.put("pk_aptclas", rs.getString(1));
//				jo.put("name", rs.getString(2));
//				jo.put("parent", rs.getString(3));
//				ja.put(jo);
//			}
//			aptitudeClassSelect = ja.toString();
//		} finally {
//			DBUtil.close(rs);
//		}
//	}

	public String getCmoneytypeSelect() {
		return cmoneytypeSelect.toString();
	}

	public String getCouttypeSelect() {
		return couttypeSelect.toString();
	}

	public String getCvenbankSelect() {
		return cvenbankSelect.toString();
	}

	public String getCompanyTypeSelect() {
		return companyTypeSelect.toString();
	}

	// private void queryCompanyTypeSelectOption(Statement stat) {
	// }

}
